﻿using System;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using System.IO;

namespace ASPNETVN.PORTAL.Modules.ADVERTISEMENT.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Item item = Components.Item.Load(this.ItemID);
                item.Delete();

                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            Components.Item item = new Components.Item();
            if (this.ItemID > 0)
            {
                item = Components.Item.Load(this.ItemID);
            }

            item.ModuleID = this.ModuleID;

            //Company
            item.Company = txtCompany.Text;

            //Link
            item.Url = txtUrl.Text.Trim();

            //Tieu de
            item.Title = txtTieuDe.Text.Trim();

            //Chi tiet
            item.Description = this.txtChiTiet.Text.Trim();

            item.Width = Convert.ToInt32(txtRong.Text);
            item.Height = Convert.ToInt32(txtCao.Text);

            item.PublishedDate = ddlPublishedDateOption.SelectedValue != "0" ? Convert.ToDateTime(txtTuNgay.Text.Trim()) : DateTime.Now;

            // Den ngay
            item.ExpiredDate = ddlExpiredDateOption.SelectedValue == "0" ? DateTime.MaxValue : Convert.ToDateTime(txtDenNgay.Text.Trim());

            //Kich hoat
            item.Status = this.chkStatus.Checked;

            item.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
            item.CreatedDate = DateTime.Now;

            //Logo quang cao
            if (fuImageUrl.HasFile)
            {
                string physicalDirectory = Server.MapPath("~/UserFiles/" + this.ModuleID);
                if (Directory.Exists(physicalDirectory) == false)
                {
                    Directory.CreateDirectory(physicalDirectory);
                }
                string physicalPath = physicalDirectory + "\\" + this.fuImageUrl.FileName;
                int i = 1;
                while (File.Exists(physicalPath))
                {
                    physicalPath = physicalDirectory + "\\" + i + "_" + this.fuImageUrl.FileName;
                    i++;
                }

                fuImageUrl.SaveAs(physicalPath);
                item.ImageUrl = "~/UserFiles/" + this.ModuleID + "/" + this.fuImageUrl.FileName;
            }

            //Insert item
            if (this.ItemID == 0)
            {
                this.ItemID = item.Insert();
            }
            else
            {
                item.ID = this.ItemID;
                item.Update();
            }

           this.ReturnToReferrerPage();       
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.ItemID > 0)
                {
                    Components.Item item = Components.Item.Load(this.ItemID);
                    if (item != null)
                    {
                        txtCompany.Text = item.Company;
                        txtTieuDe.Text = item.Title;
                        txtUrl.Text = item.Url;
                        txtChiTiet.Text = item.Description;
                        txtCao.Text = item.Height.ToString();
                        txtRong.Text = item.Width.ToString();
                        chkStatus.Checked = item.Status;

                        //Tu ngay
                        if (item.PublishedDate.ToString() != "")
                            txtTuNgay.Text = item.PublishedDate.ToString().Substring(0, item.PublishedDate.ToString().IndexOf(" "));

                        //Den ngay
                        if (item.ExpiredDate.ToString() != "")
                            txtDenNgay.Text = item.ExpiredDate.ToString().Substring(0, item.ExpiredDate.ToString().IndexOf(" "));

                    }
                }
                else
                {
                    txtTuNgay.Text = DateTime.Now.ToShortDateString();
                    txtDenNgay.Text = DateTime.MaxValue.ToShortDateString();
                    btnDelete.Visible = false;
                }
            }
        }
    }
}